﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using XmlStudio.Resources.UserControls;
using XmlStudio.Utilities;
using XmlStudio.ViewModel.Node;

namespace XmlStudio.Dialogs {
    /// <summary>
    /// Interaction logic for EditXmlElementDialog.xaml
    /// </summary>
    // ReSharper disable RedundantExtendsListEntry
    public partial class EditXmlElementDialog : Window {
        // ReSharper restore RedundantExtendsListEntry
        #region Fields
        private XmlAttributeCollectionViewModel attributes = new XmlAttributeCollectionViewModel();
        #endregion

        #region Properties
        public XmlElementViewModel XmlElement {
            get;
            private set;
        }

        public XmlAttributeCollectionViewModel Attributes {
            get { return this.attributes; }
            set { this.attributes = value; }
        }
        #endregion

        #region Constructors
        protected EditXmlElementDialog() {
            InitializeComponent();
        }

        public EditXmlElementDialog(XmlElementViewModel element)
            : this() {
            this.XmlElement = element;
            // ReSharper disable RedundantAnonymousTypePropertyName
            this.DataContext = new { Element = this.XmlElement, Attributes = this.Attributes };
            // ReSharper restore RedundantAnonymousTypePropertyName

            this.attributes.Clear();
            foreach(var attribute in element.Attributes) {
                this.attributes.Add(attribute.DeepClone());
            }
        }
        #endregion

        protected override void OnInitialized(System.EventArgs e) {
            base.OnInitialized(e);

            this.prefixTextBox.Focus();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            bool isValidationError = false;

            BindingExpression prefixExpression = prefixTextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression localNameExpression = localNameTextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression namespaceURIExpression = namespaceURITextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression attributesExpression = ucAttributes.GetBindingExpression(UcAttributes.AttributesProperty);

            if(prefixExpression != null) {
                prefixExpression.ValidateWithoutUpdate();
                if(prefixExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(localNameExpression != null) {
                localNameExpression.ValidateWithoutUpdate();
                if(localNameExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(namespaceURIExpression != null) {
                namespaceURIExpression.ValidateWithoutUpdate();
                if(namespaceURIExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(attributesExpression != null) {
                attributesExpression.ValidateWithoutUpdate();
                if(attributesExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(isValidationError) {
                // in case of validation errors, don't close the dialog
                return;
            } else {
                // when everything is ok, update the bindings and close the dialog...
                prefixExpression.UpdateSource();
                localNameExpression.UpdateSource();
                namespaceURIExpression.UpdateSource();

                this.XmlElement.Attributes.Clear();
                foreach(var att in this.Attributes) {
                    this.XmlElement.Attributes.Add(att);
                }

                attributesExpression.UpdateSource();
            }

            this.DialogResult = true;
            this.Close();
        }
    }
}